﻿param (
    [ValidateSet("Add", "Remove", "Start", "Stop", "SnapshotCreate", "SnapshotList", "SnapshotRemove", "SnapshotRestore", "DefinitionExport", "DefinitionRemove", "DefinitionImport", IgnoreCase = $true)]
	[string]$Operation,
	[string]$ConfigFile = ".\Configuration\IC.Azure.config",
	[string]$LogFile,
    [switch]$SkipCompleted
)

# Load modules
$env:dp0 = [System.IO.Path]::GetDirectoryName($myInvocation.MyCommand.Definition)
Import-Module "$env:dp0\Modules\Core\IC.Azure.Core.psm1" -DisableNameChecking -Force
Import-Module "$env:dp0\Modules\Azure\IC.Azure.Subscription.psm1" -DisableNameChecking -Force
Import-Module "$env:dp0\Modules\Azure\IC.Azure.Network.psm1" -DisableNameChecking -Force
Import-Module "$env:dp0\Modules\Azure\IC.Azure.Storage.psm1" -DisableNameChecking -Force
Import-Module "$env:dp0\Modules\Azure\IC.Azure.ReservedIP.psm1" -DisableNameChecking -Force
Import-Module "$env:dp0\Modules\Azure\IC.Azure.ResourceGroup.psm1" -DisableNameChecking -Force
Import-Module "$env:dp0\Modules\Azure\IC.Azure.NetworkSecurityGroup.psm1" -DisableNameChecking -Force
Import-Module "$env:dp0\Modules\Azure\IC.Azure.VirtualMachine.psm1" -DisableNameChecking -Force
Import-Module "$env:dp0\Modules\Azure\IC.Azure.VirtualMachine.Snapshot.psm1" -DisableNameChecking -Force
Import-Module "$env:dp0\Modules\Azure\IC.Azure.VirtualMachine.Definition.psm1" -DisableNameChecking -Force

# Display usage & help
function Display-Help
{
    Write-Text "Usage:"
    Write-Text "IC.Azure.ps1 [-Operation <operation>] [-ConfigFile <file.config>] [-LogFile <file.log>]"
    Write-Text
    Write-Text "Parameters:"
    Write-Text "-Operation`t`tSelect processing operation. Available operations are:"
    Write-Text "`t`t`t`tAdd:`t`t`t`tCreate Azure resources"
    Write-Text "`t`t`t`tRemove:`t`t`t`tRemove Azure resources"
    Write-Text "`t`t`t`tStart:`t`t`t`tStart Virtual Machines"
    Write-Text "`t`t`t`tStop:`t`t`t`tStop Virtual Machines"
    Write-Text "`t`t`t`tSnapshotCreate:`t`tCreate Virtual Machine snapshots"
    Write-Text "`t`t`t`tSnapshotList:`t`tDisplay Virtual Machine snapshots"
    Write-Text "`t`t`t`tSnapshotRemove:`t`tRemove Virtual Machine snapshots"
    Write-Text "`t`t`t`tSnapshotRestore:`tRestore Virtual Machine snapshots"
    Write-Text "`t`t`t`tDefinitionExport:`tExport Virtual Machine definitions"
    Write-Text "`t`t`t`tDefinitionRemove:`tRemove Virtual Machine definitions"
    Write-Text "`t`t`t`tDefinitionImport:`tImport Virtual Machine definitions"
    Write-Text "-ConfigFile`t`tSpecify configuration file (default: IC.Azure.config)"
    Write-Text "-LogFile`t`tSpecify log file (file.log)"
}

try
{
    Clear-Host
    Write-Text "****************"
    Write-Text "*** IC.Azure ***"
    Write-Text "****************"
    Write-Text
    if ([string]::IsNullOrEmpty($Operation))
    {
        Display-Help
    }
    else
    {
        $Global:ErrorActionPreference = "Stop"
        Start-Logging $LogFile
        Begin-Operation $Operation $ConfigFile $SkipCompleted.ToBool()
        switch ($Operation)
        {
            "Add"              { Process-SubscriptionsAdd }
            "Remove"           { Process-SubscriptionsRemove }
            "Start"            { Process-SubscriptionsStart }
            "Stop"             { Process-SubscriptionsStop }
            "SnapshotCreate"   { Process-SubscriptionsSnapshotCreate }
            "SnapshotList"     { Process-SubscriptionsSnapshotList }
            "SnapshotRemove"   { Process-SubscriptionsSnapshotRemove }
            "SnapshotRestore"  { Process-SubscriptionsSnapshotRestore }
            "DefinitionExport" { Process-SubscriptionsDefinitionExport }
            "DefinitionRemove" { Process-SubscriptionsDefinitionRemove }
            "DefinitionImport" { Process-SubscriptionsDefinitionImport }
        }
        End-Operation $Operation
    }
    Write-Text
    exit 0
}
catch 
{
	Write-Error "ERROR: $($_ | Format-List | Out-String)"
    exit -1
}
finally
{
	Finalize-Operation
}